package com.apestech.framework.file;

import com.apestech.framework.json.JSON;
import com.apestech.framework.util.DBUtil;
import com.apestech.framework.util.FileUtil;

import java.io.File;
import java.util.ArrayList;

/**
 * 带ID类型文档路径文件处理
 *
 * @author hup
 * @create 2018-08-02-14:45
 */
public class IdFile extends FileUtil {

    private String dir;
    private String key;
    private String id;
    private String file;
    private String allFile;
    private String resultFile;
    private String ext=".xml";
    private String dot;
    private boolean dirdot;

    public IdFile(String dir, String key, String ext) throws Exception {
        ini(dir, key, ".", ext);
    }

    public IdFile(String dir, String key, String dot, String ext) throws Exception {
        ini(dir, key, dot, ext);
    }

    private void ini(String dir, String key, String dot, String ext) throws Exception {
        this.dir=dir;
        this.key=key;
        this.ext="."+ext;
        this.dot=dot;
        dirdot=false;
        if(File.separator.equals(dot)){
            dirdot=true;
        }
        if(this.key.indexOf(dot)<0 && !dirdot){
            throw new Exception("传入KEY值，必须存在["+this.dot+"]分隔符！");
        }
    }

    public void getData() throws Exception {
        id="";
        if(dirdot){
            allFile=key;
            int pos=allFile.lastIndexOf(dot);
            if(pos>=0){
                file=allFile.substring(pos+1,allFile.length());
            } else {
                file=allFile;
            }
        } else {
            int pos=key.lastIndexOf(dot);
            id=key.substring(pos+1,key.length());
            allFile=key.substring(0, pos);
            pos=allFile.lastIndexOf(dot);
            file=allFile.substring(pos+1,allFile.length());
        }
        allFile=allFile.replace(dot,File.separator)+ext;
        allFile=File.separator+allFile;  //.toUpperCase();
        ArrayList<String> flist=getFieldList(dir, file+ext);
//        String msg=file+ext+"\r\n"+dir+"\r\n"+JSON.toJSONString(flist);
//        if(msg!=null) throw new Exception(msg);
        if(flist==null) return;
        resultFile=null;
        int len=flist.size();
        int count=0;
        for(int i=0; i<len; i++){
            String s=flist.get(i);  //.toUpperCase();
            if((s.toLowerCase()).indexOf(allFile.toLowerCase())>=0){
                if(count==0){
                    resultFile=FileUtil.getFileNameForSys(flist.get(i));
                    count++;
                } else if(count>0){
                    throw new Exception("该文件存在多个文件，请先指明对应唯一路径之后再调用！\r\n"
                            +"id:["+id+"]\r\n 可选列表：\r\n"
                            +JSON.toJSONString(getFileList(flist)));
                }
            }
        }
    }

    private ArrayList<String> getFileList(ArrayList<String> flist) throws Exception {
        ArrayList<String> list=new ArrayList<String>();
        int len=flist.size();
        for(int i=0; i<len; i++){
            String s=flist.get(i);
            s=s.substring(dir.length()+1, s.length()-ext.length());
            s=s.replace(File.separator,".");
            list.add(s);
        }
        return list;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public String getResultFile() {
        return resultFile;
    }

    public String getFile() {
        return file;
    }
}
